const state = {
  payStatus: {
    marriage: false, // 姻缘支付状态
    // 0:财运/性格 1:事业 2:家庭 3:健康
    other: [false, false, false, false], // 其他支付状态
    hehun: false, // 合婚支付状态
  },
};

const mutations = {
  SET_PAY_STATUS(state, { type, status, index }) {
    // marriage 和 hehun 依然是布尔值
    if (type === "marriage" || type === "hehun") {
      state.payStatus[type] = status;
    } else if (type === "other") {
      // 其他测算，index 必须是 0-3
      if (index >= 0 && index < 4) {
        state.payStatus.other[index] = status;
      }
    }
  },
  RESET_PAY_STATUS(state) {
    state.payStatus = {
      marriage: false,
      other: [false, false, false, false],
      hehun: false,
    };
  },
};

const actions = {
  // 设置支付状态
  setPayStatus({ commit }, { type, status, index }) {
    if (type === "marriage" || type === "hehun") {
      commit("SET_PAY_STATUS", { type, status });
    } else if (type === "other") {
      if (index >= 0 && index < 4) {
        commit("SET_PAY_STATUS", { type, status, index });
      } else if (index == 4) {
        // 全部赋值
        for (let i = 0; i < 4; i++) {
          commit("SET_PAY_STATUS", { type, status, index: i });
        }
      } else {
        console.error("a Invalid payment index for other:", index);
      }
    } else {
      console.error("Invalid payment type:", type);
      return;
    }
  },
  // 重置所有支付状态
  resetPayStatus({ commit }) {
    commit("RESET_PAY_STATUS");
  },
};

const getters = {
  // 获取特定类型的支付状态
  getPayStatus: (state) => (type, index) => {
    if (type === "marriage" || type === "hehun") {
      return state.payStatus[type];
    } else if (type === "other") {
      if (index >= 0 && index < 4) {
        return state.payStatus.other[index];
      } else if (index == 4) {
        return (
          Array.isArray(state.payStatus.other) &&
          state.payStatus.other.every(Boolean)
        );
      } else {
        console.error("c Invalid payment index for other:", index);
        return false;
      }
    } else {
      console.error("Invalid payment type:", type);
      return false;
    }
  },
  // 获取所有支付状态
  getAllPayStatus: (state) => state.payStatus,
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
